///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	eTLiteral.m
//	SUMMARY:	Implementation of escaped sequences for different formats
//	SUPERCLASS:	eTImage
//	INTERFACE:	None
//	PROTOCOLS:	<Annotation,HTMDSupport,ASCIISupport,LaTeXSupport,Tool,
//				InspectableTarget>
//	AUTHOR:		Rohit Khare
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//  DESCRIPTION
//		Container of rtf comment, owner, and last-modified date strings.
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	10/30/94:	Modified to support <InspectableTarget>
//	07/25/94:	Added ISINDEX subclass as an investigation into "shortcut"s
//  07/24/94:	Birth
///////////////////////////////////////////////////////////////////////////////

#import"eTLiteral.h"
#define _eTLiteralVERSION	10

@implementation eTLiteral
//int	numReps
//char 	**theReps
//int 	*theFormats

-(int) numReps
	{return NUM_FMTS_WRITTEN;}
-(const char **) theReps
	{return theReps;}
-(const char *) theRepForFormat: (int) format
	{return theReps[format];}
-setTheRep: (const char *) newRep forFormat: (int) format
{
	if(!newRep) return self;
	theReps[format] = realloc(theReps[format],(strlen(newRep)+1)*sizeof(char));
	strcpy(theReps[format],newRep);
	return self;
}


-setRep: (int) theFormat fromStream: (NXStream *)stream length:(int)length
{
	theReps[theFormat]=realloc(theReps[theFormat],length+1);
	NXRead(stream,theReps[theFormat],length);
	*(theReps[theFormat]+length)=0;
	return self;
}

// overidden etImage methods
+ toolAwake:theApp
{
	char        buf[MAXPATHLEN];
	NXBundle	*bundle;
	id 			icon=nil;

	bundle = [NXBundle bundleForClass:[eTLiteral class]];
	if ([bundle getPath:buf forResource:"eTLiteralIcon" ofType:"tiff"] ) {
		icon=[[NXImage alloc] initFromFile:buf];
		[icon setName:"eTLiteralIcon"];
	} else {
		NXLogError("Image not found: eTLiteralIcon");
	}	
	[theApp   registerAnnotation: [eTLiteral class] 
							name: "eTLiteral--HTML"
					RTFDirective: "eTLiteral"
					   menuLabel: "HTML/Literal Markup"
						 menuKey: '\0'
						menuIcon: (NXImage *) nil];
	[theApp   registerAnnotation: [eTLiteral class] 
							name: "eTLiteral--LaTeX"
					RTFDirective: "eTLiteral"
					   menuLabel: "LaTeX/Literal Markup"
						 menuKey: '\0'
						menuIcon: (NXImage *) nil];
	[theApp   registerAnnotation: [eTLiteral class] 
							name: "eTLiteral--C"
					RTFDirective: "eTLiteral"
					   menuLabel: "C/Literal Markup"
						 menuKey: '\0'
						menuIcon: (NXImage *) nil];
	// "delegate" chain:
	[eTISINDEXLiteral toolAwake:theApp];
	return self;
}

- init
{
	[super init];
	
	[self setImageComponent:[eTImageComponent newImageNamed:"eTLiteralIcon"]];
	[self setUsesButtonStyle:NO];
	[self setDraggable:YES];
	return self;
}

- free 
{	
	int i;
	for(i=0;i<[self numReps];i++)
		free(theReps[i]);
	return self = [super free];
}

- initFromPboard:thePB inDoc:theDoc linked:(BOOL) linked
{
	//[self init]; called from above.
	[super initFromPboard:thePB inDoc:theDoc linked:linked];
	[imageComponent setDoc:theDoc];
	return self;
}

- writeComponentToPath:(NXAtom)path inFormat:(int) theFormat
{
	if(!etDoc)	NXLogError("etDoc is nil at %s %u",__FILE__,__LINE__);
	return [super writeComponentToPath:path inFormat:theFormat];
}
- readRichText:(NXStream *)stream forView:view 
{
	int cnt,i;
	
	NXScanf(stream, "%d ", &i);
	if (i != _eTLiteralVERSION) {
		// bad version block.
		NXLogError("eTLiteral found unparseable version %d at position %d",
					i, NXTell(stream));
		return nil;
	}

	for(cnt=0;cnt<[self numReps];cnt++){
		NXScanf(stream, "%d", &i); NXGetc(stream); //space-eater
		if (i) {
			[self setRep:cnt fromStream: stream length:i];
			NXGetc(stream); // trailing space
		} // if i was zero, we have already advanced to the next non-white bit.
	}
	[super readRichText:stream forView:view];
	return self;
}
- writeRichText:(NXStream *)stream forView:view
{	
	int cnt;
	
	NXPrintf(stream, "%d ", _eTLiteralVERSION);
	for(cnt=0;cnt<[self numReps];cnt++) {
		if (theReps[cnt])
			NXPrintf(stream, "%d %s ",strlen(theReps[cnt]),theReps[cnt]);
		else NXPrintf(stream, "%d  ",0);
	}
	[super writeRichText:stream forView:view];
	return self;
}
- writeASCIIRef:(NXStream *)stream forView:view
{
	if(theReps[ASCII_FMT])
		NXPrintf(stream,"%s",theReps[ASCII_FMT]);
	return self;
}
- writeC:(NXStream *)stream forView:view
{
	if(theReps[C_FMT])
		NXPrintf(stream, "%s",theReps[C_FMT]);
	return self;
}
- writeHTML:(NXStream*)stream forView:view
{			
	if(theReps[HTMD_FMT])
		NXPrintf(stream,"%s",theReps[HTMD_FMT]);
	return self;
}
- writeLaTeX:(NXStream *)stream forView:view
{ 		
	if(theReps[TeXD_FMT])
		NXPrintf(stream,"%s",theReps[TeXD_FMT]);
	return self;
}
- inspect:(NXEvent *) e
{
	[[NXApp inspector] inspect:self];
	return self;
}
- (id <Inspectable>) inspectableDelegate {
	return [[eTLiteralUI new] setAnnotation:self]; }
@end